package com.gexiaobao.pigeon.ui.fragment.match

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentMatchDetailBinding
import com.gexiaobao.pigeon.ui.activity.ActivityRaceRanking
import com.gexiaobao.pigeon.ui.adapter.PassListAdapter
import com.gexiaobao.pigeon.ui.race.RaceViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 15:26
 * @Describe    : 比赛详情
 */
class FragmentMatchDetail : BaseFragment<RaceViewModel, FragmentMatchDetailBinding>() {

    private val mAdapter: PassListAdapter by lazy { PassListAdapter(arrayListOf()) }

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>
    private var mRaceId = 0
    private var racingPassId = 0
    private var mFollowStatus = 0
    private var title = "这是比赛名称"

    override fun initView(savedInstanceState: Bundle?) {
        title = arguments?.getString("title").toString()
        mRaceId = requireArguments().getInt("id")
        mFollowStatus = requireArguments().getInt("followStatus")
        racingPassId = requireArguments().getInt("racingPassId")
        mDatabind.includeBar.toolbar.initClose(title) {
            nav().navigateUp()
        }
    }

    override fun initData() {
        super.initData()
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(context, 5f))
        )

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getPassListApp(mRaceId.toString())
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getPassListApp(mRaceId.toString())
        }

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                nav().navigateAction(R.id.action_to_match_info, Bundle().apply {
                    putString("title", mAdapter.data[position].passName)
                    putInt("id", mAdapter.data[position].id)
                    putString("passStatus", mAdapter.data[position].passStatus)
                })
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.tvMatchDetailZhiding,
            mDatabind.tvMatchDetailXunfang,
            mDatabind.tvMatchDetailRanking,
            mDatabind.tvMatchDetailGroup
        ) {
            when (it) {
                mDatabind.tvMatchDetailZhiding -> {
                    nav().navigateAction(R.id.action_to_appoint_match, Bundle().apply {
                        putInt("raceId", mRaceId)
                    })
                }

                mDatabind.tvMatchDetailXunfang -> {
                    nav().navigateAction(R.id.action_to_training_and_release, Bundle().apply {
                        putInt("raceId", mRaceId)
                        putInt("followStatus", mFollowStatus)
                    })
                }

                mDatabind.tvMatchDetailRanking -> {
                    startActivity<ActivityRaceRanking>("title" to title, "raceId" to mRaceId)
                    activity?.overridePendingTransition(
                        android.R.anim.fade_in,
                        android.R.anim.fade_out
                    )
                }

                mDatabind.tvMatchDetailGroup -> {
                    nav().navigateAction(R.id.action_to_insert_group, Bundle().apply {
                        putInt("raceId", mRaceId)
                        putInt("racingPassId", racingPassId)
                    })
                }
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        loadsir.showLoading()
        mViewModel.getPassListApp(mRaceId.toString())
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.racePassList.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }
}